package com.faintlight.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class ControllerUtils {
    
    private static final Logger logger = LoggerFactory.getLogger(ControllerUtils.class);

    // ControllerUtils.java
    public static Long getCurrentUserId() {
        HttpServletRequest request = ((ServletRequestAttributes)
                RequestContextHolder.currentRequestAttributes()).getRequest();

        String userIdHeader = request.getHeader("X-User-Id");
        if (userIdHeader == null || userIdHeader.isEmpty()) {
            return 1L; // 默认用户ID
        }
        try {
            return Long.valueOf(userIdHeader);
        } catch (NumberFormatException e) {
            return 1L;
        }
    }
}
